/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.Kotxeak;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Kotxea;

public class KotxeMarkaModeloaBerria extends Panela {

	private static final long serialVersionUID = 2199834986217801900L;
	private JLabel marka;
	private JLabel modeloa;
	private JTextField mark;
	private JTextField model;
	private JButton ok;
	private JButton ezeztatu;
	private Kotxea kotx=new Kotxea();

	public KotxeMarkaModeloaBerria(String tituloa,
			final ItzuliInterfazea itzuliInterfazea) {
		super(tituloa);
		JPanel erdikoPanela = this.getErdikoPanela();

		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7, 1));

		JPanel g = new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);

		JPanel geg = new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		marka = new JLabel("Sartu marka izena:");
		marka.setFont(new Font("Arial", Font.PLAIN, 14));
		mark = new JTextField(8);
		arri.add(marka);
		arri.add(mark);
		erdikoa.add(arri);
		JPanel arrik = new JPanel();
		arrik.setBackground(SystemColor.activeCaption);
		FlowLayout flarri = new FlowLayout();
		flarri.setHgap(15);
		flarri.setVgap(10);
		arrik.setLayout(flarri);
		modeloa = new JLabel("Sartu modelo izena:");
		modeloa.setFont(new Font("Arial", Font.PLAIN, 14));
		model = new JTextField(8);
		arrik.add(modeloa);
		arrik.add(model);
		erdikoa.add(arrik);

		// beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Kotxea sortu");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);

		ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String nan = mark.getText();
				String xx = model.getText();
				if (nan.isEmpty() || xx.isEmpty()) {
					JOptionPane.showMessageDialog(null,
							"Sartu kotxe marka eta modeloa, mesedez!", "Oker",
							JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						if (kotx.existitzenDaMarka(nan)) {// marka existitzen
															// bada

							try {
								if (!kotx.existitzenDaModeloa(model.getText())) {// modeloa
																					// ez
																					// bada
																					// existitzen
									kotx.modeloaSortu(model.getText(), nan);// sortu
																			// modelo
																			// berria
																			// duen
																			// marka
									JOptionPane
											.showMessageDialog(
													null,
													" "
															+ nan
															+ " duen marka sortu da, eta modeloa "
															+ model.getText()
															+ " du ",
													"",
													JOptionPane.INFORMATION_MESSAGE);

								} else {
									JOptionPane.showMessageDialog(null,
											"Errepikatuta dago modelo hau",
											"Oker", JOptionPane.ERROR_MESSAGE);
								}
							} catch (SQLException ex) {
								JOptionPane.showMessageDialog(null,
										"Ezin izan da sartu", "Oker",
										JOptionPane.ERROR_MESSAGE);
								ex.printStackTrace();
							}
						}

						else {
							kotx.markaSortu(nan);// marka sortzen da
							kotx.modeloaSortu(model.getText(), nan);// marka
																	// duen
																	// modeloa
																	// sortu
							JOptionPane.showMessageDialog(null, " " + nan
									+ " duen marka sortu da, eta modeloa "
									+ model.getText() + " du ", "",
									JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();

						}

					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		});
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	}

	public void eremuakGarbitu() {
		mark.setText("");
		model.setText("");

	}

}
